package com.acme.bankapp.domain.bank;

import com.acme.bankapp.exceptions.IllegalAmountArgumentException;
import com.acme.bankapp.exceptions.NotEnoughFundsException;

public abstract class AbstractAccount implements Account {

	protected double balance = 0;
	
	public AbstractAccount(double amount) {
		this.balance = amount;
	}

	public long decimalValue(){
		return Math.round(balance);
	}
	
	public double getBalance() {
		return balance;
	}

	public int deposit(double amount) throws IllegalAmountArgumentException {
		if (amount <= 0) {
			throw new IllegalAmountArgumentException(balance-amount); 
		}
		balance += amount;
		return 0;
	}
	
	public int withdraw(double amount) throws IllegalAmountArgumentException, NotEnoughFundsException {
		if (amount <= 0 ) {
			throw new IllegalAmountArgumentException(amount); 
		}
		if (maximumAmountToWithdraw() < amount) {
			throw new NotEnoughFundsException(balance-amount);
		}
		
		balance -= amount;
		return 0;
	}
	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractAccount other = (AbstractAccount) obj;
		if (Double.doubleToLongBits(balance) != Double
				.doubleToLongBits(other.balance))
			return false;
		return true;
	}
	
}
